var prefix = api + "/admin/tenant";
var defaults = {
  type: 0, // 0 代表bootstrapTable 1代表bootstrapTreeTable
  contentType: "application/x-www-form-urlencoded",
  method: 'get',
  sidePagination: "server",
  sortName: "",
  sortOrder: "asc",
  createTime: true, //自定义选项，表示列表是否需要createTime排序
  pagination: true,
  pageSize: 10,
  pageList: [10, 25, 50, 100],
  toolbar: "toolbar",
  striped: false,
  escape: false,
  firstLoad: true,
  showFooter: false,
  search: false,
  showSearch: true,
  showPageGo: false,
  showRefresh: true,
  showColumns: true,
  // showToggle: true,
  showToggle: !isIE8,
  showExport: false,
  clickToSelect: false,
  singleSelect: false,
  mobileResponsive: true,
  rememberSelected: false,
  fixedColumns: false,
  fixedNumber: 0,
  rightFixedColumns: false,
  rightFixedNumber: 0,
  queryParams: queryParams,
  responseHandler: bpmResHandler,
  rowStyle: {},
};
function queryParams(params) {
  return $.common.serializeNotNull('limit=' + params.limit + '&offset=' + params.offset + '&order=asc'+ '&' + $('form').serialize());

}

function bpmResHandler(res) {
  var result = {}
  if (res) {
    result = {
      rows: res.rows,
      total: res.total
    };
  } else {
    $.modal.alertWarning('服务器错误，请联系管理员');
    result = {rows: [], total: 0};
  }
  return result
}

var nowTab = 'TAKE_EFFECTING'

$(function () {
  console.log('ddd')
  takeEffecting()
  waitEffect()
  invalid()

  $('.nav-tabs>li>a').click(function () {
    nowTab = $(this).attr('href').substr(1)
    loadList()
  })

});

function loadList() {
  $("#bootstrap-table_"+nowTab).bootstrapTable('refresh', {
    silent: true
  });
}

function takeEffecting(){
  var options = {
    id: 'bootstrap-table_TAKE_EFFECTING',
    url: api + "/scabpm/bpm/userAgencyConfig/tabList?tab=TAKE_EFFECTING",
    listType: 'bpm',
    updateUrl: '/pages/bpm/agent/agentLog',
    method: 'post',
    modalName: "生效中流程代理",
    columns: [
      {
        field: 'startDatetime',
        title: '起始时间',
      },
      {
        field: 'endDatetime',
        title: '结束时间'
      },
      {
        field: 'agencyFlowName',
        title: '代理流程'
      }
      , {
        field: 'targetUserName',
        title: '代理用户',
      }, {
        field: 'createTime',
        title: '创建时间',
      }, {
        title: '管理',
        formatter: function (value, row, index) {
          var actions = [];
          var name = '流程代理记录'
          actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="addTabFlow(\'' + row.id + '\',\'' + name + '\')"><i class="fa fa-edit"></i>代理记录</a> ');
          actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="cancelFlow(\'' + row.id + '\')"><i class="fa fa-remove"></i>取消</a> ');
          return actions.join('');
        }
      }]
  };
  // $.table.init(options);
  var optionsAll = Object.assign({},defaults,options)
  $('#' + options.id).bootstrapTable(optionsAll)
}

function addTabFlow(id, name) {
  var title = '流程代理记录'
  $.modal.openTab(title, '/pages/bpm/agent/agentLog?id='+id);
}

function cancelFlow(id){
  $.modal.confirm("确定取消吗？", function () {
    $.operate.post(api+'/scabpm/bpm/userAgencyConfig/disable?id='+id,{},function (res) {
      if (res.isOk){
        loadList()
        $.modal.msgSuccess(res.msg == null ? '操作成功' : res.msg);
      }else{
        $.modal.alertError(res.msg ? res.msg : '错误！');
      }

    },false)
  });
}

function waitEffect(){
  var options = {
    id: 'bootstrap-table_WAIT_EFFECT',
    url: api + "/scabpm/bpm/userAgencyConfig/tabList?tab=WAIT_EFFECT",
    listType: 'bpm',
    updateUrl: '/pages/bpm/agent/edit',
    method: 'post',
    modalName: "待生效流程代理",
    columns: [
      {
        field: 'startDatetime',
        title: '起始时间',
      },
      {
        field: 'endDatetime',
        title: '结束时间'
      },
      {
        field: 'agencyFlowName',
        title: '代理流程'
      }
      , {
        field: 'targetUserName',
        title: '代理用户',
      }, {
        field: 'createTime',
        title: '创建时间',
      }, {
        title: '管理',
        formatter: function (value, row, index) {
          var actions = [];
          var name = '流程代理【'+row.name+'】'
          actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="removeFlow(\'' + row.id + '\')"><i class="fa fa-remove"></i>删除</a> ');

          return actions.join('');
        }
      }]
  };
  var optionsAll = Object.assign({},defaults,options)
  $('#' + options.id).bootstrapTable(optionsAll)
}

function removeFlow(id){
  $.modal.confirm("确定删除吗？", function () {
    $.operate.post(api+'/scabpm/bpm/userAgencyConfig/remove?id='+id,{},function (res) {
      if (res.isOk){
        loadList()
        $.modal.msgSuccess(res.msg == null ? '操作成功' : res.msg);
      }else{
        $.modal.alertError(res.msg ? res.msg : '错误！');
      }

    },false)
  });
}

function invalid(){
  var options = {
    id: 'bootstrap-table_INVALID',
    url: api + "/scabpm/bpm/userAgencyConfig/tabList?tab=INVALID",
    listType: 'bpm',
    updateUrl: '/pages/bpm/agent/edit',
    method: 'post',
    modalName: "已失效流程代理",
    columns: [
      {
        field: 'startDatetime',
        title: '起始时间',
      },
      {
        field: 'endDatetime',
        title: '结束时间'
      },
      {
        field: 'agencyFlowName',
        title: '代理流程'
      }
      , {
        field: 'targetUserName',
        title: '代理用户',
      },{
        field: 'enable',
        title: '失效方式',
        formatter: function (value, row, index) {
          return '<i class="fa text-danger">'+(value ? '自动失效' : '手动失效')+'</i>';
        }
      }, {
        field: 'createTime',
        title: '创建时间',
      }, {
        title: '管理',
        formatter: function (value, row, index) {
          var actions = [];
          var name = '流程代理【'+row.name+'】'
          actions.push('<a class="btn btn-success btn-xs" href="javascript:void(0)" onclick="addTabFlow(\'' + row.id + '\',\'' + name + '\')"><i class="fa fa-edit"></i>代理记录</a> ');

          return actions.join('');
        }
      }]
  };
  var optionsAll = Object.assign({},defaults,options)
  $('#' + options.id).bootstrapTable(optionsAll)
}

function add() {
  layer.open({
    type: 2,
    area: ['930px', ($(window).height() - 50)+'px'],
    fix: false,
    //不固定
    maxmin: true,
    shade: 0.3,
    title: '添加流程',
    content: '/pages/bpm/agent/edit'+'?tab='+nowTab,
    btn: ['确定', '关闭'],
    // 弹层外区域关闭
    shadeClose: true,
    yes: function(index,layero){
      var iframeWin = layero.find('iframe')[0];
      iframeWin.contentWindow.submitHandler(index, layero);
    },
    cancel: function (index) {
      return true;
    }
  });
}
